<template>
  <div class="login-container">
    <!-- 表单结构 -->
    <el-form ref="myform" :rules="rules" class="myform" :model="form">
      <h3>
        <img src="../../assets/common/login-logo.png" alt="" />
      </h3>
      <el-form-item prop="mobile">
        <el-input
          v-model="form.mobile"
          prefix-icon="el-icon-user-solid"
        ></el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          prefix-icon="el-icon-lock"
          show-password
          type="password"
          v-model="form.password"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button
          :loading="loading"
          @click="login"
          class="mybtn"
          type="primary"
          >登录</el-button
        >
      </el-form-item>
      <p>还没账号?去注册</p>
    </el-form>
  </div>
</template>

<script>
import { validateMobile } from "@/utils/validate";
import { mapActions } from "vuex";
export default {
  data() {
    return {
      //加载效果
      loading: false,
      //表单的数据源
      form: {
        mobile: "13800000002",
        password: "123456",
      },
      //校验规则
      rules: {
        mobile: [
          { required: true, message: "用户名不能为空", trigger: "blur" },
          //自定义验证
          { validator: validateMobile }, //validateMobile封装了手机正则校验
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 4, max: 10, message: "密码长度4-10", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    ...mapActions({
      userLogin: "user/login",
    }),
    login() {
      //正在加载
      this.loading = true;
      //完成表单校验
      this.$refs.myform.validate(async (res) => {
        if (res) {
          try {
            //校验通过:提交数据到服务器
            //调用vuex中的方法
            await this.userLogin(this.form);
            //接收 _redirect
            const _redirect = this.$route.query._redirect;
            //判断是否存在参数 _redirect
            if (_redirect) {
              this.$router.push(_redirect);
              console.log("登陆", res);
            } else {
              // 跳转到首页
              this.$router.push("/"); //同步代码
              //提示登陆成功
              this.$message.success("欢迎回来");
            }
          } catch (err) {
            console.log(err);
            console.log("登陆时出错:" + err.message);
          } finally {
            this.loading = false;
          }
        }
      });
    },
  },
};
</script>

<style lang="scss">
.login-container {
  height: 100%;
  background-image: url("~@/assets/common/login.jpg"); // 设置背景图片
  background-position: center; // 将图片位置设置为充满整个屏幕
  .myform {
    width: 520px;
    padding: 0 35px;
    margin: 0 auto;
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    h3 {
      text-align: center;
      margin-bottom: 40px;
    }
    .el-input__inner {
      height: 50px;
      color: #79b0fe;
      background-color: #d4e5ff;
    }
    .mybtn {
      width: 100%;
      height: 60px;
      font-size: 20px;
      background-color: #407ffe;
    }
    p {
      text-align: center;
      color: #fff;
    }
  }
}
</style>
